import 'dart:io';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

/// 个人资料头像组件
class ProfileAvatar extends StatelessWidget {
  final String? avatarUrl;
  final File? imageFile; // 本地选择的头像文件
  final String userName;
  final double radius;
  final VoidCallback? onTap;
  final Color? backgroundColor;
  final Color? foregroundColor;
  final double? fontSize;

  const ProfileAvatar({
    Key? key,
    this.avatarUrl,
    this.imageFile,
    required this.userName,
    this.radius = 30.0,
    this.onTap,
    this.backgroundColor,
    this.foregroundColor,
    this.fontSize,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final effectiveBackgroundColor = backgroundColor ?? Theme.of(context).primaryColor.withOpacity(0.1);
    final effectiveForegroundColor = foregroundColor ?? Theme.of(context).primaryColor;
    final effectiveFontSize = fontSize ?? radius * 0.8;

    Widget avatarContent;

    if (imageFile != null) {
      // 优先显示本地选择的文件
      avatarContent = ClipOval(
        child: Image.file(
          imageFile!,
          width: radius * 2,
          height: radius * 2,
          fit: BoxFit.cover,
        ),
      );
    } else if (avatarUrl != null && avatarUrl!.isNotEmpty) {
      // 显示网络图片
      avatarContent = CachedNetworkImage(
        imageUrl: avatarUrl!,
        imageBuilder: (context, imageProvider) => CircleAvatar(
          radius: radius,
          backgroundImage: imageProvider,
        ),
        placeholder: (context, url) => CircleAvatar(
          radius: radius,
          backgroundColor: effectiveBackgroundColor,
          child: const SizedBox(
            width: 20,
            height: 20,
            child: CircularProgressIndicator(strokeWidth: 2),
          ),
        ),
        errorWidget: (context, url, error) => _buildDefaultAvatar(
          effectiveBackgroundColor,
          effectiveForegroundColor,
          effectiveFontSize,
        ),
      );
    } else {
      // 显示默认的文字头像
      avatarContent = _buildDefaultAvatar(
        effectiveBackgroundColor,
        effectiveForegroundColor,
        effectiveFontSize,
      );
    }

    return InkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(radius),
      child: Stack(
        alignment: Alignment.center,
        children: [
          avatarContent,
          if (onTap != null) // 如果可点击，显示编辑图标提示
            Positioned(
              bottom: 0,
              right: 0,
              child: Container(
                padding: const EdgeInsets.all(4),
                decoration: BoxDecoration(
                  color: Colors.black54,
                  borderRadius: BorderRadius.circular(radius),
                ),
                child: const Icon(Icons.edit, color: Colors.white, size: 12),
              ),
            ),
        ],
      ),
    );
  }

  /// 构建默认的文字头像
  Widget _buildDefaultAvatar(Color bgColor, Color fgColor, double size) {
    return CircleAvatar(
      radius: radius,
      backgroundColor: bgColor,
      child: Text(
        userName.isNotEmpty ? userName.substring(0, 1).toUpperCase() : '?',
        style: TextStyle(
          fontSize: size,
          fontWeight: FontWeight.bold,
          color: fgColor,
        ),
      ),
    );
  }
} 